package com.thinkersre.controller;

import com.thinkersre.entity.Event;
import com.thinkersre.event.EventProducer;
import com.thinkersre.util.ThinkersReUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import static com.thinkersre.util.ThinkersReConstant.TOPIC_SHARE;

@Controller
public class ShareController {
    private static final Logger logger = LoggerFactory.getLogger(ShareController.class);
    @Resource
    private EventProducer eventProducer;
    @Value("${thinkersre.path.domain}")
    private String domain;
    @Value("${server.servlet.context-path}")
    private String contextPath;
    @Value("${wk.image.storage}")
    private String wkImageStorage;




    @Value("${qiniu.bucket.share.url}")
    private String shareBucketUrl;
    @GetMapping("/share")
    @ResponseBody
    public String share(String htmlUrl){
        //文件名
        String fileName = ThinkersReUtil.getUUID();
        //异步生成长图
        Event event = new Event()
                .setTopic(TOPIC_SHARE)
                .setData("htmlUrl",htmlUrl)
                .setData("fileName",fileName)
                .setData("suffix",".png");
        eventProducer.fireEvent(event);
        //返回访问路径
        Map<String,Object> map = new HashMap<>();
//        map.put("shareUrl",domain+contextPath+"/share/image/"+fileName);
        map.put("shareUrl",shareBucketUrl + "/" + fileName);
        return ThinkersReUtil.getJSONString(0,null,map);
    }




    //废弃
    //获取长图
    @GetMapping("/share/image/{fileName}")
    public void getShareImage(@PathVariable("fileName") String fileName, HttpServletResponse httpServletResponse){
        if(StringUtils.isBlank(fileName)){
            throw new IllegalArgumentException("文件名不能为空.");

        }

        httpServletResponse.setContentType("image/png");
        File file = new File(wkImageStorage+"/"+fileName+".png");
        try {
            OutputStream outputStream = httpServletResponse.getOutputStream();
            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int b = 0;
            while((b=fileInputStream.read(buffer)) != -1){
                outputStream.write(buffer,0,b);
            }
        } catch (IOException e) {
            logger.error("获取长图失败: "+e.getMessage());
        }

    }
}
